﻿using Entity;
using Entity.Room;
using IHotelBLL.IRoomBLL;
using IHotelDAL.IRoomDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HotelBLL.RoomBLL
{
    public class RoomTypeBLL : IRoomTypeBLL
    {
        IRoomTypeDAL _roomTypeDAL;
        public RoomTypeBLL(IRoomTypeDAL roomType)
        {
            _roomTypeDAL = roomType;
        }

        public bool AddRoomType(string typeName, out string msg)
        {

            

            //通过类别名词去找类别信息
            RoomType roomType = _roomTypeDAL.GetEntities().FirstOrDefault(c => c.TypeName == typeName);
            if (roomType != null)
            {
                msg = "类型已存在";
                return false;
            }

            RoomType floor = new RoomType()
            {
                Id = Guid.NewGuid().ToString(),
                TypeName = typeName,
            };
            bool isSuccess = _roomTypeDAL.CreateEntity(floor);
            msg = isSuccess ? "添加成功" : "添加失败";
            return isSuccess;
        }

        public RoomType GetRoomTypeById(string id)
        {
            return _roomTypeDAL.GetEntities().FirstOrDefault(u => u.Id == id);
        }

        public List<SelectOptionModel> GetRoomTypeOptions()
        {
            return _roomTypeDAL.GetEntities().Select(c => new SelectOptionModel()
            {
                Id = c.Id,
                Name = c.TypeName
            }).ToList();
        }

       

        public List<RoomType> GetRoomTypes(int page, int limit, string typeName, out int count)
        {
            var datas = _roomTypeDAL.GetEntities();
            count = datas.Count();
            var roomInfos = datas.Skip((page - 1) * limit).Take(limit).Select(f => new
            {
                f.Id,
                f.TypeName
            }).ToList();
            return roomInfos.Select(f => new RoomType()
            {
                Id = f.Id,
                TypeName = f.TypeName,
            }).ToList();
        }

        public bool RemoveRoomType(string id, out string msg)
        {
            bool isSuccess = _roomTypeDAL.DeleteEntity(id);
            msg = isSuccess ? "删除成功" : "删除失败";
            return isSuccess;
        }

        public bool UpdateRoomType(string id, string typeName, out string msg)
        {
            RoomType roomInfo = GetRoomTypeById(id);
            if (roomInfo == null)
            {
                msg = "未找到客房ID";
                return false;
            }
            roomInfo.TypeName = typeName;


            bool isSuccess =_roomTypeDAL.UpdateEntity(roomInfo);
            if (isSuccess)
            {
                msg = "修改成功";
                return true;
            }
            else
            {
                msg = "修改失败";
                return true;
            }
        }
      

      
        //public bool AddRoomType(RoomType roomType, out string msg)
        //{
        //    roomType.Id = Guid.NewGuid().ToString();
        //    bool isSuccess = _roomTypeDAL.CreateEntity(roomType);
        //    msg = isSuccess ? "添加成功" : "添加失败";
        //    return isSuccess;
        //}

        //public List<RoomType> GetAllRoomTypes()
        //{
        //    throw new NotImplementedException();
        //}

        //public bool RemoveRoomType(string id, out string msg)
        //{
        //    bool isSuccess = _roomTypeDAL.DeleteEntity(id);
        //    msg = isSuccess ? "删除成功" : "删除失败";
        //    return isSuccess;
        //}

        //public bool UpdateRoomType(RoomType roomType, out string msg)
        //{
        //    bool isSuccess = _roomTypeDAL.UpdateEntity(roomType);
        //    msg = !isSuccess ? "修改失败" : "修改成功";
        //    return isSuccess;

        //}
    }
}
